<?php
require_once 'PHPThumb/src/ThumbLib.inc.php';

class Lib2k_Image_Image {

    private $_imageDirectory;
    private $_imagePathTemp;
    private $_thumb; // img object
    private $_returnFilePath; // path for db
    private $_fileExtension;
    private $_dirMaxFileCount = 100;

	public function __construct($pathToImage)
    {
        $this->_imagePathTemp = $pathToImage;
        try {
            $this->_thumb = PhpThumbFactory::create($pathToImage);    
        } catch (Exception $e){
            $this->_thumb = PhpThumbFactory::create(THUMBLIB_BASE_PATH . "/default.jpg"); // default image 
        }
        
        $this->_fileExtension = $this->getFileExtension($pathToImage);
        $this->_imageDirectory =  APPLICATION_PATH . "/../www/site/content/images/";
        $this->_returnFilePath =  "/site/content/images/";  // return path for img url
    }

    // getting file extension from path
    private function getFileExtension($filePath)
    {
        $path_parts = pathinfo($filePath);
        $ext = $path_parts['extension'];
        if ($ext == "jpeg") {
            $ext = "jpg";
        }elseif($ext == "png"){
        	$ext = "png";
        }
        return $ext;
    }

    private function generateFileName()
    {
        return md5(uniqid(rand(),1));
    }

    private function getSubFolderForSave() {
        $dirMaxFileCount = $this->_dirMaxFileCount;
        $dirPath = $this->_imageDirectory;
        $dirs = scandir($dirPath);
        //var_dump($dirs);echo "<br/>";
        foreach($dirs as &$dir){ // dirs names to int
            $dir = intval($dir);
        };
        //var_dump($dirs);
        $maxDirName = max($dirs);// get max int dirname
        $maxDirPath = $dirPath . max($dirs);
      //  echo "<br />" . $maxDirName . "<br />";
        if(!file_exists($maxDirPath)){
            mkdir($maxDirPath, 0700); // creating dir if not exists
        }
        $subDir = scandir($maxDirPath);
        if ((count($subDir) - 2) >= $dirMaxFileCount) { // if count of files > maxdirsize
            $returnMaxDirName = $maxDirName + 1;
            $returnMaxDirPath = $dirPath . $returnMaxDirName;
            mkdir($returnMaxDirPath, 0700); // creating new dir            
        } else {
            $returnMaxDirName = $maxDirName;
            $returnMaxDirPath = $dirPath . $maxDirName;            
        }
        return $returnMaxDirName;        
    }

    //saving image
    private function save()
    {
        $filenameNew = $this->generateFileName();
        $subFolder = $this->getSubFolderForSave();
        try {
            $this->_thumb->save($this->_imageDirectory
                                . $subFolder . "/"
                                . $filenameNew
                                . "."
                                . $this->_fileExtension,
                                $this->_fileExtension);
            return $this->_returnFilePath . $subFolder . "/" . $filenameNew . "." . $this->_fileExtension;
        } catch (Exception $e){
            return null;   
        }

    }
    public function removeTempFile(){
    	return unlink($this->_imagePathTemp);
    }
/** ======================================================================== */
/** Write own methods for creating thumbs  */

    public function profileImageThumb()
    {
        $this->_thumb->adaptiveResize(152, 152);
        return $this->save();

    }

	public function contentImageThumb(){
        $this->_thumb->adaptiveResize(153, 153);
        return $this->save();

    }
    public function saveOrig(){
    	return $this->save();
    }
}